package studentsinfomanagement.view.administrator;

import studentsinfomanagement.bean.Course;
import studentsinfomanagement.dao.CourseDAO;
import studentsinfomanagement.dao.TeacherDAO;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

/**
 * 管理员管理课程信息类

 */
public class AdministratorCourseCenter extends JFrame {

    private  AdministratorCourseCenter administratorCourseCenter = null;
    private CourseDAO courseDAO = null;//课程数据信息管理对象
    private TeacherDAO teacherDAO = null;//教师数据信息管理对象
    private DefaultTableModel tableModel = null;
    private JTable table = null;
    private JScrollPane scrollPane = null;
    private String[] columnNames;
    private String[][] tableVales;


    {
        table = new JTable();
    }

    public AdministratorCourseCenter(){
        administratorCourseCenter = this;
        courseDAO = CourseDAO.getInstance();
        teacherDAO =TeacherDAO.getInstance();

        this.setTitle("管理课程信息");//标题
        this.setSize(550,500);//大小
        this.setLocationRelativeTo(null);//居中
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//关闭操作隐藏界面
        this.setResizable(false);//大小不可调节

        BorderLayout bl = new BorderLayout();
        this.setLayout(bl);

        //界面元素
        //课程滚动显示区域
        showAllCourses();//获取全部授课信息
        scrollPane = new JScrollPane(table);

        //退出选课中心
        JPanel jp_exit = new JPanel();
        JButton btn_exit = new JButton("退出");
        jp_exit.add(btn_exit);

        //将组件添加到界面
        this.add(scrollPane,BorderLayout.CENTER);
        this.add(jp_exit,BorderLayout.SOUTH);

        //界面可见
        this.setVisible(true);

        //监听器
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                int sel_row = table.getSelectedRow();
                int sel_column = table.getSelectedColumn();
                if(sel_row >= 0 && sel_column == 4){//过滤掉非法值
                    String cno = (String) table.getValueAt(sel_row,0);
                    Course course = new Course();
                    course.setCno(cno);
                    boolean result = courseDAO.delete(course);
                    if(!result){//删除失败
                        JOptionPane.showMessageDialog(administratorCourseCenter,"删除课程失败！");
                    }else {//删除成功
                        JOptionPane.showMessageDialog(administratorCourseCenter,"删除课程成功！");
                        showAllCourses();
                        revalidate();
                    }
                }
            }
        });

        btn_exit.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                administratorCourseCenter.setVisible(false);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }

    public void showAllCourses(){
        List<Course> courseList = courseDAO.getCourses();
        columnNames = new String[]{"课程号","课程名","授课教师","学分","操作"};
        tableVales = new String[courseList.size()][];
        int i = 0;
        for(Course c:courseList){
            String cno = c.getCno();
            String name = c.getName();
            String tno = c.getTno();
            String tName = teacherDAO.getTeacherByTno(tno).getName();
            Integer score = c.getScore();
            String s_score = score.toString();
            tableVales[i] = new String[5];
            tableVales[i][0] = cno;
            tableVales[i][1] = name;
            tableVales[i][2] = tName;
            tableVales[i][3] = s_score;
            tableVales[i][4] = "删除";
            i++;
        }
        tableModel = new DefaultTableModel(tableVales,columnNames){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.setModel(tableModel);
    }

}
